package com.gosophia.conferenceManagement.summary.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.common.dao.DocumentDao;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.service.impl.ConferenceHandleServiceImpl;
import com.gosophia.conferenceManagement.summary.dao.SummaryJobDataDao;
import com.gosophia.conferenceManagement.summary.entity.SummaryJobData;
import com.gosophia.conferenceManagement.summary.service.SummaryService;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;

/**
 * 功能：会议总结service接口实现
 * @author mayue
 *
 */
@Service("summaryService")
@Transactional
public class SummaryServiceImpl extends ConferenceHandleServiceImpl  implements SummaryService{

    @Autowired
    private TaskManagementService taskManagementService;
    @Autowired
    private SummaryJobDataDao summaryJobDataDao;
    @Autowired
    private DocumentDao documentDao;
    
    @Override
    public void saveOrUpdateSummaryJobData(SummaryJobData summaryJobData,List<Document> attachements,Document onlineDocument) throws BusinessException{
        Assert.notNull(summaryJobData,"summaryJobData不能为null");
        
//        Job job = taskManagementService.findJobDetailByJobId(summaryJobData.getJob().getJobId());
       
        SummaryJobData summaryJobDataLoad = (SummaryJobData) taskManagementService.findJobDataDetail(summaryJobData.getJob().getJobId(), SummaryJobData.class);
        
        summaryJobDataDao.clearSession();
        
        if(summaryJobDataLoad==null){
            summaryJobData.setAttachments(attachements);
            summaryJobData.setOnlineDocument(onlineDocument);
            summaryJobDataDao.save(summaryJobData);
            
        }else{
            List<Document> docs = summaryJobDataDao.getListByNamedQuery("summaryService.findDocByJobId", summaryJobData.getJob().getJobId());
            if(docs!=null){
                for(Document doc : docs){
                    documentDao.delete(doc);
                }
            }
            summaryJobDataDao.clearSession();
            summaryJobDataLoad.setAttachments(attachements);
            summaryJobDataLoad.setOnlineDocument(onlineDocument);
            summaryJobDataLoad.setNextApproval(summaryJobData.getNextApproval());
            summaryJobDataLoad.setOpinion(summaryJobData.getOpinion());
            summaryJobDataLoad.setTurningAction(summaryJobData.getTurningAction());
            summaryJobDataDao.update(summaryJobDataLoad);
        }
    }
    @Override
    public void deleteAllDocumentById(Long jobId) {
        Assert.notNull(jobId,"jobId不能为null");
        List<Document> docs = summaryJobDataDao.getListByNamedQuery("summaryService.findDocByJobId", jobId);
        if(docs!=null){
            for(Document doc : docs){
                documentDao.delete(doc);
            }
        }
    }
    @Override
    public void autoSaveSummaryJobData(SummaryJobData summaryJobData) throws BusinessException {
        Assert.notNull(summaryJobData,"summaryJobData不能为null");
        
//        Job job = taskManagementService.findJobDetailByJobId(summaryJobData.getJob().getJobId());
       
        SummaryJobData summaryJobDataLoad = (SummaryJobData) taskManagementService.findJobDataDetail(summaryJobData.getJob().getJobId(), SummaryJobData.class);
        
        summaryJobDataDao.clearSession();
        
        if(summaryJobDataLoad==null){
            summaryJobDataDao.save(summaryJobData);
            
        }else{
            summaryJobDataLoad.setOnlineDocument(summaryJobData.getOnlineDocument());
            summaryJobDataLoad.setNextApproval(summaryJobData.getNextApproval());
            summaryJobDataLoad.setOpinion(summaryJobData.getOpinion());
            summaryJobDataLoad.setTurningAction(summaryJobData.getTurningAction());
            summaryJobDataDao.update(summaryJobDataLoad);
        }
        
    }
   
}
